﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;

public partial class Police_Question_View : System.Web.UI.Page
{
    private string[] charts = { "Column", "Spline", "StepLine", "Spline" };
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }
    private void Bind()
    {

        if (Request["ID"] == null || Request["DepartmentID"] == null)
            return;
        //DataView dv = SplitPage.ExecuteSQL("Select (Select ShortName From FireMis..Account_Departments where FireMis..Account_Departments.DepartmentID=##tt.DepartmentID) as ShortName ,Score,DepartmentID  From (Select e.DepartmentID as DepartmentID,sum(Score)/((Select max(mark) From SecurityEvaluate..SecurityOption where pid ='" + new Guid(Request["ID"]) + "')* SecurityEvaluate.dbo.GetDepartmentIDCountt1(e.DepartmentID,'" + Request["ID"] + "')) as Score From SecurityEvaluate..SecurityResult as s right join SecurityEvaluate..Examination as e on e.DepartmentID=s.SetDepartment And PID='" + new Guid(Request["ID"]) + "' And Left(e.DepartmentID,4)='" + Request["DepartmentID"] + "' Group by e.DepartmentID) ##tt where Left(DepartmentID,4)='" + Request["DepartmentID"] + "' order by Score desc ").Tables[0].DefaultView;

        DataView dv = SplitPage.ExecuteSQL("Select  (Select ShortName from FireMis..Account_Departments Where FireMis..Account_Departments.DepartmentID=##t4.DepartmentID) ShortName,Score,DepartmentID from (Select DepartmentID,((Select Sum(Score) From  SecurityEvaluate..SecurityResult2 where setdepartment=DepartmentID and  pid='"+ new Guid(Request["ID"]) +"')/((Select Sum(Score) From  (Select max(mark) as Score From SecurityEvaluate..SecurityOption2 where pid  in (Select pID From SecurityEvaluate..SecurityResult2 where pid= '" + new Guid(Request["ID"]) + "' and setDepartment=departmentID  ) Group by pid ) ##t0) * SecurityEvaluate.dbo.GetDepartmentIDCountt1D(DepartmentID,'" + new Guid(Request["ID"]) + "'))) as Score  From SecurityEvaluate..Examination2 Where Left(DepartmentID,4)='" + Request["DepartmentID"].Substring(0, 4) + "' and sid='"+Request["sid"]+"' group by DepartmentID)##t4  Order by Score Desc").Tables[0].DefaultView;


        Chart1.Series["未超出警戒线单位"].Points.DataBindXY(dv, "ShortName", dv, "Score");

        Question2DataContext db = new Question2DataContext();
        var obj = (from o in db.SecurityProblem2 where o.Id == new Guid(Request["ID"]) select o).FirstOrDefault();

        decimal Standard = obj.GuardCount.Value;

        // add a series to the chart
        Series series = Chart1.Series.Add("警戒线");
        series.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), charts[Chart1.Series.Count - 1], true);
        series.BorderWidth = 1;
        series.BorderColor = Color.Red;   
        series.ShadowOffset = 0;
        series.Color = Color.Red;        
        int j = 0;
        string Department = string.Empty;

        while (j++ < Chart1.Series["未超出警戒线单位"].Points.Count)
        {
            double temp = double.Parse(Standard.ToString());
            series.Points.Add(temp / 100);          
           
        }
        for (int i = 0; i < dv.Table.Rows.Count-1; i++)
        {
            if (dv.Table.Rows[i][1] == DBNull.Value)
                break;
            
                if (decimal.Parse(dv.Table.Rows[i][1].ToString())*100 >= Standard)
                {
                    Department += dv.Table.Rows[i][0] + "、";
                    Chart1.Series["未超出警戒线单位"].Points[i].Color = Color.FromArgb(200, 0, 0);
                }
            
        }
        if (Department != string.Empty)
        {
            Department = Department.Remove(Department.Length - 1);
            lbljinggao.Text = "<font color=red>警告：</font>" + Department + "已超出警戒线";
        }
        Chart1.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
        Chart1.Series["未超出警戒线单位"].LabelFormat = "p0";

        //RemoveButton.Enabled = Chart1.Series.Count > 1;
        //AddButton.Enabled = Chart1.Series.Count < charts.Length;
        lblRemark.Text = "<font color=green>备注：</font>"+"如果各大中队超出警戒线柱状图将变为红色";
    }

}
